﻿using System;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    /* This part of formRotaEditor contains methods responsible for adjusting the interface
     * (location and size of controls)
     * When formRotaEditor window is resized. */
    public partial class MainForm
    {
        // The panel On the "RotaPackaging" tab        
        private double _panelsProportions;

        public void Hourglass(bool On)
        {
            Cursor = On ? Cursors.WaitCursor : Cursors.Default;            
        }

        #region EVENTS - all events that we need to respond to by adjusting controls.

        private void tbMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            layoutAdjustWholeUI();
        }

        private void splPanels_SplitterMoved(object sender, SplitterEventArgs e)
        {
            if (!btnHideTemplate.Visible)
            {
                return;
            }
            SaveSplitterPanelPosition();
            layoutAdjustWholeUI();
        }

        private void SaveSplitterPanelPosition()
        {
            _panelsProportions = (double)splPanels.SplitterDistance /
                (double)splPanels.Height;                                
        }

        private void RetrieveSplitPanelProportions()
        {
            splPanels.SplitterDistance = Convert.ToInt16((double) this.splPanels.Height*_panelsProportions);
        }

        private void formRotaEditor_Resize(object sender, EventArgs e)
        {   
            layoutAdjustWholeUI();            
            layoutAdjustWidthOfSheets(true);            
        }

        
        #endregion

        #region METHODS - all the methods that take care of resizing, repositioning or otherwise altering the look of the controls
        private void layoutAdjustWholeUI()
        {            
            Hourglass(true);
            SuspendLayout();
            if (tblTempToolbar.ColumnStyles[5].Width != 28)
            {
                tblTempToolbar.ColumnStyles[5].Width = 28;
            }
                        
            /* Which tab is (or has just been made) active now?
             * 
             * We only adjust controls On the visible tab.
             * When user switches tabs, this method gets called anyway,
             * so everything will be fixed "just On Time". */
            var tab = tbMain.SelectedIndex + 1;
            /* - we AddAnotherRowString 1 just for convenience - I prefer thinking of tabs
             * as 1, 2, 3 etc. rather than 0, 1, 2. */

            switch (tab)
            {
                // tab 1 ("Calendar")
                case 1:
                    {                        
                        layoutAdjustWidthOfSheets(true);
                        break;
                    }
                // tab 2 ("RotaPackaging")
                case 2:
                    {                           
                        AdjustSheetTempRotaHeight();                                                
                        svTemplate.Width = tblTempToolbar.Width;
                        sheetTempOpeningHrs.Width = svTemplate.Width;                                                
                        if (btnShowTemplate.Visible)
                        {
                            /* Even if panel2 is collapsed (and therefore of fixed size)
                             * - we still have to adjust the size of panel1 accordingly.
                             * 
                             * We need to disable the SplitterMoved event though, otherwise
                             * our adjustment will trigger this event again,
                             * which in turn will call this method again...
                             * thus creating an infinite loop. */                            
                            if (splPanels.SplitterDistance != splPanels.Height - splPanels.Panel2MinSize)
                            {
                                var e = new SplitterEventHandler(splPanels_SplitterMoved);
                                splPanels.SplitterMoved -= e;
                                if (splPanels.Height > 0)
                                {
                                    splPanels.SplitterDistance = splPanels.Height - splPanels.Panel2MinSize;
                                }
                                splPanels.SplitterMoved += e;
                            }                            
                        }
                        break;
                    }
            }            
            ResumeLayout();
            Hourglass(false);
        }
        
                

        private void AdjustSheetTempRotaHeight()
        {
            var _spaceForSheetTempRota = splPanels.Panel2.Height -
                sheetTempOpeningHrs.Height -
                tblTempToolbar.Height - 2;
            svTemplate.SetNaturalHeight(_spaceForSheetTempRota);            
            sheetTempOpeningHrs.Top = svTemplate.Bottom - 2;    
        }

        

        private void layoutAdjustWidthOfSheets(bool fromTop)
        {
            // 
            #region suspending layout
            svRota.SuspendLayout();
            svTemplate.SuspendLayout();            
            sheetTempOpeningHrs.SuspendLayout();
            #endregion


            /* svRota and svTemplate are supposed to be "coordinated" in terms
             * of columns width. When user resizes - for example - the Tuesday column
             * of one datagridview, the other one is supposed to adjust.
             * 
             * the ColumnWidthChanged event handlers of both DataGridViews call this method.
             * to accomplish our goal, we have to temporarily disable these event handlers
             * here.             
             * otherwise we get an infinite recursion as both datagridviews keep On triggering
             * eachother'pShift event:
             * (- _howManyWeHave've just been set to 107 pixels, adjust yourself
             * - and _howManyWeHave've just been set to 107 pixels, you adjust yourself
             * - and _howManyWeHave've just...) - etc. :) */
            #region disabling event handlers
            // 
            var _sr = new DataGridViewColumnEventHandler(sheetRota_ColumnWidthChanged);
            var _str = new DataGridViewColumnEventHandler(sheetTempRota_ColumnWidthChanged);
            svRota.ColumnWidthChanged -= _sr;
            svTemplate.ColumnWidthChanged -= _str;
            #endregion

            if (fromTop)
            {
                colEmployeeT.Width = colEmployee.Width;
            }
            else
            {
                colEmployee.Width = colEmployeeT.Width;
            }
            for (var i = 0; i < 7; i++)
            {
                if (fromTop)
                {
                    if (svTemplate.Columns[i].Width != svRota.Columns[i].Width)
                    {
                        svTemplate.Columns[i].Width = svRota.Columns[i].Width;
                    }
                }
                else
                {
                    if (svRota.Columns[i].Width != svTemplate.Columns[i].Width)
                    {
                        svRota.Columns[i].Width = svTemplate.Columns[i].Width;
                    }
                }
                if (sheetTempOpeningHrs.Columns.Count > i)
                {
                    sheetTempOpeningHrs.Columns[i].Width = svRota.Columns[i].Width;
                }
            }
            #region resuming layout and restoring event handlers
            svRota.ResumeLayout();
            svTemplate.ResumeLayout();            
            sheetTempOpeningHrs.ResumeLayout();            
            svRota.ColumnWidthChanged += _sr;
            svTemplate.ColumnWidthChanged += _str;
            #endregion

        }                        
        #endregion

        private void UpdateTitlebar()
        {
            Text = Resources.FORM_MAINFORM_Title.Replace(
                "#",
                Program.Logged.ToString()).Replace(
                    "$",
                    Program.CurrentFile.FileNameNoPath);
        }

        private void CollapseStaffDetails()
        {
            splStaffLeft.Panel2Collapsed = false;
        }

        /// <remarks>
        /// Does not work if currently logged user hasn't got enough privileges! (User.CanViewStaff)
        /// </remarks>
        private void UncollapseStaffDetails()
        {
            splStaffLeft.Panel2Collapsed = !Program.Logged.CanViewStaff;
        }
    }        
}
